package com.jeesite.modules.kj.api.controller.member;


import com.jeesite.common.collect.ListUtils;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.response.*;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.*;
import com.jeesite.modules.kj.service.KjBadgeService;
import com.jeesite.modules.kj.service.KjBadgeTypeService;
import com.jeesite.modules.kj.service.KjMemberBadgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "徽章api", tags = {"徽章API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/badge")
public class BadgeApiController {

    @Autowired
    KjBadgeService kjBadgeService;

    @Autowired
    KjBadgeTypeService kjBadgeTypeService;
    @Autowired
    KjMemberBadgeService kjMemberBadgeService;


    @ApiOperation(value = "获取徽章列表", notes = "获取徽章列表")
    @GetMapping(value = "list")
    public ResponseBean<List<BadgeTypeResponseVo>> list() {
        List<BadgeTypeResponseVo> vos = ListUtils.newArrayList();
        List<KjBadgeType> typeList = kjBadgeTypeService.findList(new KjBadgeType());
        for (KjBadgeType badgeType: typeList
             ) {
            BadgeTypeResponseVo vo = new BadgeTypeResponseVo(badgeType);
            KjBadge where = new KjBadge();
            where.setKjBadgeType(badgeType);
            List<KjBadge> badgeList = kjBadgeService.findList(where);
            vo.setBadgeList(badgeList);
            vos.add(vo);
        }
        return new ResponseBean<>(vos);
    }


    @ApiOperation(value = "浏览徽章", notes = "浏览徽章")
    @GetMapping(value = "view")
    @UserLoginToken
    public ResponseBean<String> view(@ApiIgnore @CurrentUser String memberId, String id) {
        KjMemberBadge kjMemberBadge = kjMemberBadgeService.get(id);
        if(memberId.equals(kjMemberBadge.getMemberId())){
            kjMemberBadge.setIsView(KjMemberBadge.ISVIEW);
            kjMemberBadgeService.save(kjMemberBadge);
            return new ResponseBean<>("浏览成功");
        }
        return new ResponseBean<>();

    }



    @ApiOperation(value = "我的所有勋章", notes = "我的所有勋章")
    @GetMapping(value = "my")
    @UserLoginToken
    public ResponseBean<List<MemberBadgeResponseVo>> my(@ApiIgnore @CurrentUser String memberId) {
        KjMemberBadge where = new KjMemberBadge();
        where.setMemberId(memberId);
        List<KjMemberBadge> kjMemberBadges = kjMemberBadgeService.findList(where);
        List<MemberBadgeResponseVo> results = ListUtils.newArrayList();
        for (KjMemberBadge mb: kjMemberBadges
             ) {
            results.add(new MemberBadgeResponseVo(mb));
        }
        return new ResponseBean<>(results);

    }
}
